# !/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
from email.header import Header
from email.mime.text import MIMEText

from loguru import logger


def get_html(datas):
    header = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
    # https://xueqiu.com/S/SZ001234
    th = f"<body text='#000000'><table bgcolor='#B0E0E6' style=' font-size: 10px;'border='1' cellspacing='0' cellpadding='0' bordercolor='#000000' width='100%' align='center' >" \
         "<tr  bgcolor='#F79646' align='left' >" \
         f"<th style='width:200px'>行业</th>" \
         f"<th style='width:250px'>名称代码</th>" \
         f"<th style='width:200px'>谷底跌幅/avg</th>" \
         f"<th style='width:200px'>谷底日价/今日价</th>" \
         "</tr>"
    tr = ""
    # 谷底日期,谷底价,谷底跌幅
    index = 0
    # datas = datas if len(datas) < 11 else datas[:10]
    for index, item in enumerate(datas):
        sname = item.get('名称')
        snumber = item.get('代码')
        indus = item.get('行业')
        t_pct = round(item.get('今日涨幅'), 2)
        low_date = item.get('谷底日期')
        low_price = item.get('谷底价')
        today_price = item.get('价格')
        avg = round(item.get('avg'), 2)
        low_pct = round(item.get('谷底跌幅') * 100, 2)
        low_pct_pre = round(item.get('谷底跌幅_pre') * 100, 2)
        low_pct2 = (f'今日:-{low_pct}%'
                   f'<br>昨日:-{low_pct_pre}%'
                   f'<br>avg:-{avg}%'
                   f'<br>'
                   )

        zc_info = (f'日期:{low_date}'
                   f'<br>谷价:{low_price}'
                   f'<br>今价:{today_price}'
                   f'<br>今日涨幅:{t_pct}%'
                   f'<br>'
                   )
        if t_pct > 0:
            text_color = '#FF0000'
            # bg_color = '#B0E000'
        elif t_pct < 0:
            text_color = '#00FF00'
            # bg_color = '#B0E0E6'
        else:
            text_color = '#FFA500'
            # bg_color = '#B0E0E6'

        if low_pct < low_pct_pre:
            bg_color = '#FFFFFF'
        else:
            bg_color = '#B0E0E6'

        stock_number_arr = snumber.split('.')
        stock_number = f'{stock_number_arr[1]}{stock_number_arr[0]}'
        h_a = f"<a href='https://xueqiu.com/S/{stock_number}'>{stock_number}<br>{sname}</a>"
        td = ''
        td = td + f"<td style='background-color:{bg_color}; color:{text_color}'>" + str(indus) + "</td>"
        td = td + f"<td style='background-color:{bg_color}; color:{text_color}'>" + str(h_a) + "</td>"
        td = td + f"<td style='background-color:{bg_color}; color:{text_color}'>" + str(low_pct2) + "</td>"
        td = td + f"<td style='background-color:{bg_color}; color:{text_color}'>" + str(zc_info) + "</td>"
        tr = tr + "<tr>" + td + "</tr>"

    body = str(tr)
    tail = f'</table></body></html>'
    html_txt = header + th + body + tail
    return index + 1, html_txt


def send_html(datas, title):
    # 邮箱服务器地址
    # 发件人邮箱地址
    # 发件人邮箱密码(使用申请的客户端授权码代替)
    smtp_server = 'smtp.qq.com'
    from_addr = '83852084@qq.com'
    # passwd = 'ruqbqiqjmnjhbgih'
    passwd = 'iqjzamocyiaibjfg'

    # 收件人邮箱地址
    to_addr = 'stockbing@icloud.com'

    # 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)
    # msg = MIMEText(str(data), 'plain', 'utf-8')
    cnt, html = get_html(datas)
    msg = MIMEText(html, _subtype='html', _charset='utf-8')

    # 设置邮件主题
    title = str(title).replace('gen_low_rank_industry【', f'行业【{cnt}/')
    msg['Subject'] = Header(title, charset='utf-8')
    msg['from'] = from_addr
    msg['to'] = to_addr

    # 获取SMTP对象
    # server = smtplib.SMTP(host=smtp_server, port=25)
    server = smtplib.SMTP_SSL(host=smtp_server, port=465)
    # 登录163邮箱服务器
    server.login(user=from_addr, password=passwd)
    # 发送邮件
    server.sendmail(from_addr=from_addr, to_addrs=to_addr, msg=msg.as_string())
    # 退出邮箱服务器
    server.quit()
    logger.info(f'{title} 邮件发送成功')
